/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.entity;

import java.io.File;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import webshop.beans.admin.FUBean;

import database.entitymanager.ShopImageManager;
import database.entitymanager.ShopItemManager;

/**
 * 
 * @author Stefan
 */
@Entity
@Table(name = "shop_item")
@NamedQueries({ @NamedQuery(name = "ShopItem.findAll", query = "SELECT s FROM ShopItem s"),
		@NamedQuery(name = "ShopItem.findByItemid", query = "SELECT s FROM ShopItem s WHERE s.itemid = :itemid"),
		@NamedQuery(name = "ShopItem.findByItemprice", query = "SELECT s FROM ShopItem s WHERE s.itemprice = :itemprice"),
		@NamedQuery(name = "ShopItem.findByItemname", query = "SELECT s FROM ShopItem s WHERE s.itemname = :itemname"),
		@NamedQuery(name = "ShopItem.findByItemamount", query = "SELECT s FROM ShopItem s WHERE s.itemamount = :itemamount"),
		@NamedQuery(name = "ShopItem.findByItemrating", query = "SELECT s FROM ShopItem s WHERE s.itemrating = :itemrating") })
public class ShopItem implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Basic(optional = false)
	@Column(name = "ITEMID")
	private Integer itemid;
	@Basic(optional = false)
	@Column(name = "ITEMPRICE")
	private BigDecimal itemprice;
	@Basic(optional = false)
	@Column(name = "ITEMNAME")
	private String itemname;
	@Lob
	@Column(name = "ITEMDESCRIPTION")
	private String itemdescription;

	@Basic(optional = false)
	@Column(name = "ITEMAMOUNT")
	private int itemamount;
	@Column(name = "ITEMRATING")
	private Integer itemrating;
	@JoinTable(name = "relationship_4", joinColumns = { @JoinColumn(name = "ITEMID", referencedColumnName = "ITEMID") }, inverseJoinColumns = { @JoinColumn(name = "ITEMGROUPID", referencedColumnName = "ITEMGROUPID") })
	@ManyToMany(fetch = FetchType.EAGER)
	private List<ItemGroup> itemGroupList;
	@ManyToMany(mappedBy = "shopItemList", fetch = FetchType.LAZY)
	private List<Action> actionList;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "shopItem", fetch = FetchType.LAZY)
	private List<CartItem> cartItemList;
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "shopItem", fetch = FetchType.LAZY)
	private List<ShopImage> shopImageList;

	public ShopItem() {
	}

	public String getTumbnailImage() {
		ShopItem si = ShopItemManager.findShopItem(this.getItemid());
		String imageURL = "/pages/static/images/no_image.png";

		for (int i = 0; i < si.getShopImageList().size(); i++) {
			if ((new File(FUBean.contextPath + si.getShopImageList().get(i).getShopimagerelativepath())).exists()) {
				return imageURL = si.getShopImageList().get(i).getShopimageurl();
			} else {
				try {
					ShopImageManager.destroy(si.getShopImageList().get(i).getShopimageid());
					System.out.println("Image not found , deleting and trying another");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		return imageURL;
	}

	public String[] getImages() {
		// TODO: Namestiti da ovo ustvari funkcionise
		ShopItem si = ShopItemManager.findShopItem(this.getItemid());
		return new String[2];
	}

	public ShopItem(Integer itemid) {
		this.itemid = itemid;
	}

	public ShopItem(BigDecimal itemprice, String itemname, String itemdescription, int itemamount, int itemrating) {
		super();
		this.itemid = (int) Math.round(Math.random() * 100);
		this.itemprice = itemprice;
		this.itemname = itemname;
		this.itemdescription = itemdescription;
		this.itemamount = itemamount;
		this.itemrating = itemrating;
	}

	public ShopItem(Integer itemid, BigDecimal itemprice, String itemname, int itemamount) {
		this.itemid = itemid;
		this.itemprice = itemprice;
		this.itemname = itemname;
		this.itemamount = itemamount;
	}

	public Integer getItemid() {
		return itemid;
	}

	public void setItemid(Integer itemid) {
		this.itemid = itemid;
	}

	public BigDecimal getItemprice() {
		return itemprice;
	}

	public void setItemprice(BigDecimal itemprice) {
		this.itemprice = itemprice;
	}

	public String getItemname() {
		return itemname;
	}

	public void setItemname(String itemname) {
		this.itemname = itemname;
	}

	public String getItemdescription() {
		return itemdescription;
	}

	public void setItemdescription(String itemdescription) {
		this.itemdescription = itemdescription;
	}

	public int getItemamount() {
		return itemamount;
	}

	public void setItemamount(int itemamount) {
		this.itemamount = itemamount;
	}

	public Integer getItemrating() {
		return itemrating;
	}

	public void setItemrating(Integer itemrating) {
		this.itemrating = itemrating;
	}

	public List<ItemGroup> getItemGroupList() {
		return itemGroupList;
	}

	public void setItemGroupList(List<ItemGroup> itemGroupList) {
		this.itemGroupList = itemGroupList;
	}

	public List<Action> getActionList() {
		return actionList;
	}

	public void setActionList(List<Action> actionList) {
		this.actionList = actionList;
	}

	public List<CartItem> getCartItemList() {
		return cartItemList;
	}

	public void setCartItemList(List<CartItem> cartItemList) {
		this.cartItemList = cartItemList;
	}

	public List<ShopImage> getShopImageList() {
		return shopImageList;
	}

	public void setShopImageList(List<ShopImage> shopImageList) {
		this.shopImageList = shopImageList;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (itemid != null ? itemid.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		// TODO: Warning - this method won't work in the case the id fields are
		// not set
		if (!(object instanceof ShopItem)) {
			return false;
		}
		ShopItem other = (ShopItem) object;
		if ((this.itemid == null && other.itemid != null) || (this.itemid != null && !this.itemid.equals(other.itemid))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "database.entity.ShopItem[itemid=" + itemid + "]";
	}

}
